import axios from 'axios'
/* Toast VantUI的提示方法 */
import { Toast } from 'vant'
import store from '@/store'

// 不带加载状态的请求
const http = axios.create({
  baseURL: 'https://m.maizuo.com'
})
/* 请求拦截器 */
http.interceptors.request.use((config) => {
  /* 设置请求头 */
  /* 在请求头中加入城市id */
  config.headers['X-Client-Info'] = `{"a":"3000","ch":"1002","v":"5.2.0","e":"1648814973149091199746049","bc":"${store.state.CityModule.cityId}"}`
  return config
}, (error) => {
  Toast.fail('请求失败')
  return Promise.reject(error)
})
/* 响应拦截器 */
http.interceptors.response.use((response) => {
  return response
}, (error) => {
  Toast.fail('响应失败')
  return Promise.reject(error)
})

// 带加载状态的请求(两者的区别就是是否带Vant的加载状态)
const load = axios.create({
  baseURL: 'https://m.maizuo.com'
})

load.interceptors.request.use((config) => {
  config.headers['X-Client-Info'] = `{"a":"3000","ch":"1002","v":"5.2.0","e":"1648814973149091199746049","bc":"${store.state.CityModule.cityId}"}`
  Toast.loading({
    message: '加载中...',
    forbidClick: true,
    overlay: true,
    duration: 0
  })
  return config
}, (error) => {
  Toast.clear()
  Toast.fail('请求失败')
  return Promise.reject(error)
})

load.interceptors.response.use((response) => {
  /* 响应成功则清除加载状态 */
  Toast.clear()
  return response
}, (error) => {
  Toast.fail('响应失败')
  return Promise.reject(error)
})

export { http, load }

export default { http, load }
